package com.woniu.service.Impl;

import com.woniu.dao.*;
import com.woniu.service.SearchBankService;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.enums.SubjectEnum;
import com.woniuxy.commons.enums.TestBankEnum;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @program: bzeducation
 * @author: Xingqilin
 * @create: 2020-07-16 19:54
 * @description: 题库查询服务
 */
@Service
public class SearchBankServiceImpl implements SearchBankService {

    @Resource
    SelectBankDao selectBankDao;
    @Resource
    VacancyBankDao vacancyBankDao;
    @Resource
    MaterialBankDao materialBankDao;
    @Resource
    ClozeBankDao clozeBankDao;
    @Resource
    JudgeBankDao judgeBankDao;
    @Resource
    ReviseBankDao reviseBankDao;
    @Resource
    WriterBankDao writerBankDao;
    @Resource
    MaterialQuestionDao materialQuestionDao;
    @Resource
    KnowledgeDao knowledgeDao;

    @Cacheable(value = "findBankById", unless = "#result == null")
    public <K> K findBankById(TestBankEnum testBank, Long id) {
        K resultQuestion = null;
        switch (testBank) {
            case SELECT_BANK:
                resultQuestion = (K) selectBankDao.findSelectById(id);
                break;
            case VACANCY_BANK:
                resultQuestion = (K) vacancyBankDao.findVacancyById(id);
                break;
            case CLOZE_BANK:
                resultQuestion = (K) clozeBankDao.findClozeById(id);
                break;
            case MATERIAL_BANK:
                MaterialBank materialById = materialBankDao.findMaterialById(id);
                assemble(materialById);
                resultQuestion = (K) materialById;
                break;
            case JUDGE_BANK:
                resultQuestion = (K) judgeBankDao.findJudgeById(id);
                break;
            case REVISE_BANK:
                resultQuestion = (K) reviseBankDao.findReviseById(id);
                break;
            case WRITER_BANK:
                resultQuestion = (K) writerBankDao.findWriterById(id);
                break;
            default:
                throw new IllegalStateException("FindBankById:未找到该问题所属题库，请联系管理员! " + testBank);
        }
        return resultQuestion;
    }

    @Cacheable(value = "findBankByTopic", unless = "#result == null")
    public List findBankByTopic(SubjectEnum subject, TestBankEnum testBank, String topic) {
        List resultQuestionList = null;
        String fuzzy = "%" + topic + "%";
        switch (testBank) {
            case SELECT_BANK:
                resultQuestionList = (List) selectBankDao.findSelectByTopic(subject.getIndex(), fuzzy);
                break;
            case VACANCY_BANK:
                resultQuestionList = (List) vacancyBankDao.findVacancyByTopic(subject.getIndex(), fuzzy);
                break;
            case CLOZE_BANK:
                resultQuestionList = (List) clozeBankDao.findClozeByTopic(subject.getIndex(), fuzzy);
                break;
            case MATERIAL_BANK:
                List<MaterialBank> materialByTopic = materialBankDao.findMaterialByTopic(subject.getIndex(), fuzzy);
                for (MaterialBank materialBank : materialByTopic) {
                    assemble(materialBank);
                }
                resultQuestionList = (List) materialByTopic;
                break;
            case JUDGE_BANK:
                resultQuestionList = (List) judgeBankDao.findJudgeByTopic(subject.getIndex(), fuzzy);
                break;
            case REVISE_BANK:
                resultQuestionList = (List) reviseBankDao.findReviseByTopic(subject.getIndex(), fuzzy);
                break;
            case WRITER_BANK:
                resultQuestionList = (List) writerBankDao.findWriterByTopic(subject.getIndex(), fuzzy);
                break;
            default:
                throw new IllegalStateException("findSelectByTopic:未找到该问题所属题库，请联系管理员! " + testBank);
        }
        return resultQuestionList;
    }

    @Cacheable(value = "findBankByParams", unless = "#result == null")
    public HashMap<Long, Object> findBankByParams(SubjectEnum subject, TestBankEnum testBank, QueryConditions queryConditions) {
        HashMap<Long, Object> resultQuestionMap = new HashMap<>();
        switch (testBank) {
            case SELECT_BANK:
                List<SelectBank> selectByParams = selectBankDao.findSelectByParams(queryConditions);
                for (SelectBank selectByParam : selectByParams) {
                    selectByParam.setDisplayKnow(knowledgeDao.selectKnowledge(selectByParam.getSelectKnowledge()));
                    resultQuestionMap.put(selectByParam.getSelectId(), selectByParam);
                }
                break;
            case VACANCY_BANK:
                List<VacancyBank> vacancyByParams = vacancyBankDao.findVacancyByParams(queryConditions);
                for (VacancyBank vacancyByParam : vacancyByParams) {
                    vacancyByParam.setDisplayKnow(knowledgeDao.selectKnowledge(vacancyByParam.getVacancyKnowledge()));
                    resultQuestionMap.put(vacancyByParam.getVacancyId(), vacancyByParam);
                }
                break;
            case CLOZE_BANK:
                List<ClozeBank> clozeByParams = clozeBankDao.findClozeByParams(queryConditions);
                for (ClozeBank clozeByParam : clozeByParams) {
                    clozeByParam.setDisplayKnow(knowledgeDao.selectKnowledge(clozeByParam.getClozeKnowledge()));
                    resultQuestionMap.put(clozeByParam.getClozeId(), clozeByParam);
                }
                break;
            case MATERIAL_BANK:
                List<MaterialBank> materialByParams = materialBankDao.findMaterialByParams(queryConditions);
                for (MaterialBank materialByParam : materialByParams) {
                    assemble(materialByParam);

                    resultQuestionMap.put(materialByParam.getMaterialId(), materialByParam);
                }
                break;
            case JUDGE_BANK:
                List<JudgeBank> judgeByParams = judgeBankDao.findJudgeByParams(queryConditions);
                for (JudgeBank judgeByParam : judgeByParams) {

                    resultQuestionMap.put(judgeByParam.getJudgeId(), judgeByParam);
                }
                break;
            case REVISE_BANK:
                List<ReviseBank> reviseByParams = reviseBankDao.findReviseByParams(queryConditions);
                for (ReviseBank reviseByParam : reviseByParams) {

                    resultQuestionMap.put(reviseByParam.getReviseId(), reviseByParam);
                }
                break;
            case WRITER_BANK:
                List<WriterBank> writerByParams = writerBankDao.findWriterByParams(queryConditions);
                for (WriterBank writerByParam : writerByParams) {

                    resultQuestionMap.put(writerByParam.getWriterId(), writerByParam);
                }
                break;
            default:
                throw new IllegalStateException("findSelectByParams:未找到该问题所属题库，请联系管理员! " + testBank);
        }
        return resultQuestionMap;
    }

    private void assemble(MaterialBank materialBank) {
        materialBank.setSelectBanks(materialQuestionDao.findSelectById(materialBank.getMaterialId()));
        materialBank.setVacancyBanks(materialQuestionDao.findVacancyById(materialBank.getMaterialId()));
        materialBank.setJudgeBanks(materialQuestionDao.findJudgeById(materialBank.getMaterialId()));
    }


}
